const model = require('../models/userModel');

var userModel = model.registerModel;

// 创建用户
async function createUser(user) {
    try {
        var result = await userModel.create(user);
        // console.log(result);
        return {
            success: true,
            user:user
        }
    } catch (error) {
        return {
            success: false,
            msg: 'datmsgabase error!'+error
        }
    }
    
}


// 通过用户名检索用户是否存在
async function findByName(user) {
    try {
    var result = await userModel.findOne({ name: user.name });
    // console.log(result);
    if (result) {
        return {
            success: true,
            user:result
        }
    } else {
        return {
            success: false,
            msg:'please register first ^_^'
        }
    }
    } catch (error) {
        return {
            success: false,
            msg: 'database error!'+error
        }
    }
    
}

module.exports = {
    findByName: findByName,
    createUser: createUser
}